package weibo.tools;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class ImageCollector {
	public static void main(String[] args) {
		for(int i = 1 ; i<1000;i++){
			//proxy setting
//			HttpHost proxy = new HttpHost("connsvr.nike.com",8080);
			HttpClient httpClient = new DefaultHttpClient();
			
//			httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy);
			
			HttpUriRequest httpUriRequest = new HttpGet("http://3g.sina.com.cn/prog/wapsite/sso/register.php?backURL=http%3A%2F%2Fweibo.cn%2F&backTitle=%D0%C2%C0%CB%CE%A2%B2%A9&vt=4&revalid=2&ns=1&type=m");
			
			HttpResponse httpResponse = null;
			try {
				httpResponse = httpClient.execute(httpUriRequest);
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			HttpEntity entity = httpResponse.getEntity();
			String html = null;
			try {
				html = new String(EntityUtils.toString(entity).getBytes());
				
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			Document doc = Jsoup.parse(html);
			String url =  doc.getElementsByTag("img").get(0).attr("src");
			
			try {
				downloadPic(url, i);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("the count: "+ i+"-----------");
		}
	}
	
	private static void downloadPic(String url, int count) throws Exception{
		URL uri = new URL(url);
		InputStream in = new BufferedInputStream(uri.openStream());
		String mimeType = URLConnection.guessContentTypeFromStream(in);
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		byte[] buf = new byte[1024];
		int n = 0;
		while (-1!=(n=in.read(buf)))
		{
		   out.write(buf, 0, n);
		}
		out.close();
		in.close();
		byte[] response = out.toByteArray();
		String folderpath = "/Users/henrylian/Desktop/lab";
		String fileName = "show" + String.valueOf(count)+"."+mimeType.substring(mimeType.indexOf("/")+1);
		File  dirFile = new File(folderpath);
		boolean bFile   = dirFile.exists();
         if( bFile == true )
         {
            System.out.println("The folder exists.");
         }
         else
         {
            System.out.println("The folder do  not exist,now trying to create a one...");

            bFile = dirFile.mkdirs();
            if( bFile == true )
            {
               System.out.println("Create successfully!");
            }
            else
            {
                System.out.println("Disable to make the folder,please check the disk is full or not.");
            }
         }
		FileOutputStream fos = new FileOutputStream(folderpath+ "/"+fileName);
		fos.write(response);
		fos.close();
	}
}
